﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.ArrivalNotice
{
    public class ArrivalNoticesBackHandler : IRequestHandler<ArrivalNoticesBackCommand, ResultsDTO<ArrivalNoticesDto>>
    {
        #region 仓储 
        private readonly IBateRepository<ArrivalNoticesDto> ArrivalNoticeRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ArrivalNoticeRepository"></param>
        public ArrivalNoticesBackHandler(IBateRepository<ArrivalNoticesDto> ArrivalNoticeRepository)
        {
            this.ArrivalNoticeRepository = ArrivalNoticeRepository;
        }
        #endregion

        #region 到货通知 反填
        /// <summary>
        /// 到货通知 反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<ArrivalNoticesDto>> Handle(ArrivalNoticesBackCommand request, CancellationToken cancellationToken)
        {
            //初始化一个ResultDTO<ArrivalNoticesDto>对象result，并设置Code为 0
            ResultsDTO<ArrivalNoticesDto> result = new ResultsDTO<ArrivalNoticesDto>() { Code = 0 };

            //连接mySql 
            string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

            //sql语句
            string str = $"select * from arrivalnotice where ArrivalNoticeId = {request.ArrivalNoticeId} ";

            ArrivalNoticesDto dto = new ArrivalNoticesDto();
            //创建DapperHelper对象 用于与数据库进行交互
            DapperHelper helper = new DapperHelper();
            //调用ArrivalNoticeRepository方法 （）里面是执行查询并获取结果 能查询到的条件Picking数据存储在result对象中
            result.Objects = ArrivalNoticeRepository.GetAll(str, dto, conn).FirstOrDefault();
            result.Message = "生产领料反填成功";

            return result;
        }
        #endregion

    }
}
